/**
 * 帮助模块
 * @author
 * @date 2012-11-20
 */

var tag=false;
var colNames = ['','主题','提问人','提问人照片','提问时间','提问内容','阅读数','回复数','id','提问者id'];

/**
 * colModel 显示列的属性
 *
 * @type {Object}
 */
var colModel = [
	{ name:'first', index:'first', width:"100%", align:"left", title:false },
	{ name:'title',index:'title', hidden:true }, 	
	{ name:'askerName',index:'askerName', hidden:true },  
	{ name:'askerPortrait',index:'askerPortrait', hidden:true },  
	{ name:'formatPublishTime',index:'formatPublishTime', hidden:true }, 
	{ name:'content',index:'content', hidden:true },
	{ name:'readCount',index:'readCount', hidden:true },
	{ name:'replyCount',index:'replyCount', hidden:true },
	{ name:'id',index:'id', hidden:true },
	{ name:'askerId',index:'askerId', hidden:true },
];

/**
 * rowNum 想要显示的数据行数
 *
 * @type {Number}
 */

var rowNum = 10;

/**
 * total_last 用于判定表格的总页码是否发生了改变
 *
 * @type {Number}
 */

var total_last = 0;

/**
 * loadDataSuccess 判定是否成功加载完数据
 *
 * @type {Boolean}
 */

var loadDataSuccess = false;

$( "#help-table" ).jqGrid({
	url:'../help/loadQuestions.html',
	datatype:"json",
	mtype: "POST",
	height: "auto",
	autowidth: true,
	colNames: colNames, 
	colModel: colModel,
	rowNum: rowNum, 
	viewrecords: true,
	emptyrecords : '暂时无人发表话题',
	pager: 'helppager', 
	jsonReader: {
		root: "result",
		page: "page",
		total: "total",
		records: "records",
		repeatitems: false,
		cell: "cell",
		id: "id"
	},
	
	gridComplete:function() {
		// 取消外边框
		$( "#gbox_help-table" ).css( "border", "none" );
		
		// 隐藏表头
		$( "#gview_help-table .ui-jqgrid-hdiv" ).css( "display", "none" );
		
		// 取消列间的分界限
		$( "#gview_help-table .ui-jqgrid-bdiv td" ).css( "border-right-style", "none" );

		// 改变底边框样式
		$( "#gview_help-table .ui-jqgrid-bdiv tr:not(:first-child) td" ).css( "border-bottom", "1px solid #E5E5E5" );
		
		// 取消最后一行的底边框
		$( "#gview_help-table .ui-jqgrid-bdiv tr:last-child td" ).css( "border-bottom-style", "none" );
		
		// 设置表格行高
		//$( "#gview_help-table .ui-jqgrid-bdiv tr:gt(0)" ).css( "height", "110px" );

		/**
		 * IDs 获取当前页所有数据的id
		 *
		 * @type {Object}
		 */

		var IDs = $( "#help-table" ).jqGrid( "getDataIDs" );
		
		//没有数据
		if(IDs.length==0){
			$("#helppager_center").hide();
			$(".ui-paging-info").css("text-align","left");
			$(".ui-paging-info").css("font-size","15px");
			$(".ui-jqgrid-view").css("height","60px");
		}
		
		for (var i=0; i < IDs.length; i++) {

			/**
			 * everyRowData 根据某一行数据的id获取该数据所有信息
			 *
			 * @type {Object}
			 */

			var everyRowData = $( "#help-table" ).jqGrid( "getRowData", IDs[i] );
			
			// 帮助信息自定义模块
			var metadata ='<li>' +
								'<h3>' +
									'<img src="../images/help/help_icon1.png" width="20px" height="17px"/>' +
									'<span class="center-tit" style="text-decoration:underline; cursor:pointer;" onclick="jump()">' + everyRowData.title +
									'</span>' +
								'</h3>' +
								'<div class="comment">' +
									'<p class="author">提问人：<a href="../user/other.html?otherId='+everyRowData.askerId+'" title="查看TA的主页">' + everyRowData.askerName + 
										'</a>&nbsp <span id="comTime_0"> ' + everyRowData.formatPublishTime + 
										'</span> &nbsp|  阅读（'+everyRowData.readCount+'） | 回复 （'+everyRowData.replyCount+'）</p>' +
								
								'</div>' +
							'</li>';

			$( "#help-table" ).jqGrid( 'setRowData', IDs[i], { first: metadata } );
			// 取消提示信息
			$( ".metadata" ).parent("td").attr("title","");
	
			
        }		
	},
	onSelectRow: function(rowid) {
		if(tag){
		var everyRowData = $( "#help-table" ).jqGrid( "getRowData", rowid );
		var askerPortrait=everyRowData.askerPortrait;
		var title=everyRowData.title;
		var id=everyRowData.id;
		var content=everyRowData.content;
		var askerName=everyRowData.askerName;
		var publishTime=everyRowData.formatPublishTime;
		var readCount=everyRowData.readCount;
		//清空内容
		$(".help-content").empty();
		
		//给URL添加参数
		if(document.location.hash=""){
			window.location.hash="#num=0";
		}
		//加载每个问题的详细内容
		$(".help-content").load(
			"../help/questionDetail.html?id="+id+"&readCount="+readCount,
			null,
			function(response, status, xml) {
				if ( status !== "success" ) {
					alert("加载失败！");
				}else{
					document.getElementById('title').innerHTML=title;
					document.getElementById('askerName').innerHTML=askerName;
					document.getElementById('publishTime').innerHTML=publishTime;
					document.getElementById('content').innerHTML=content;
					$(".topic-photo img").attr("src","../"+askerPortrait);
					$("#qid").val(id);
					$("#search").hide();
				}
			}
		);	
		window.location.hash = "#num=" + id;

	}
	
}});
function jump(){
	tag=true;
}

